﻿using IOP.SgrA;
using IOP.SgrA.ECS;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;

namespace VkSample82
{
    public class StarSystem : ComponentSystem
    {
        private readonly ILogger<StarSystem> _Logger;
        private float starAngle;
        private readonly Vector3 Y = new Vector3(0, 1, 0);

        public StarSystem(ILogger<StarSystem> logger)
        {
            _Logger = logger;
        }

        public override void Initialization()
        {
            int index = 0;
            ContextManager.Foreach<StarGroup>((group) =>
            {
                Context context = ContextManager.GetContext(group.Entity);
                if (index == 0) context.SetCustomPropery("pointSize", 2.0f);
                else context.SetCustomPropery("pointSize", 4.0f);
            });
        }

        public override void Update(TimeSpan lastStamp)
        {
            try
            {
                starAngle += 0.02f;
                if (starAngle >= 360) starAngle = 0;
                ContextManager.Foreach<StarGroup>((group) =>
                {
                    Context context = ContextManager.GetContext(group.Entity);
                    ref MVPMatrix mvp = ref context.GetMVPMatrix();
                    mvp.ModelMatrix = Matrix4x4.CreateFromAxisAngle(Y, starAngle * MathF.PI / 180.0f);
                    context.PushToRenderGroup();
                });
            }
            catch (Exception e)
            {
                _Logger.LogError(e.Message);
            }
        }

        public struct StarGroup
        {
            public Entity Entity;
            public StarComponent Star;
            public TransformComponent Transform;
        }
    }
}
